package com.sais.manage.boot.platform.exception;

import com.sais.common.entity.Result;
import com.sais.manage.boot.building.exception.BuildingException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.session.UnknownSessionException;
import org.postgresql.util.PSQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 全局异常处理
 * @author: Gnerv LiGen
 * @date: 2018-08-01
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * 权限异常处理
     */
    @ExceptionHandler(value = AuthorizationException.class)
    @ResponseBody
    public Result authorizationExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_405);
    }
    /**
     * 认证异常处理
     */
    @ExceptionHandler(value = AuthenticationException.class)
    @ResponseBody
    public Result AuthenticationExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_400,e.getMessage());
    }

    /**
     * 会话异常处理
     */
    @ExceptionHandler(value = UnknownSessionException.class)
    @ResponseBody
    public Result unknownSessionExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_401);
    }

    /**
     * 业务异常处理
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Result businessExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_400, e.getMessage());
    }

    /**
     * 宿舍楼管理模块业务异常处理
     */
    @ExceptionHandler(value = BuildingException.class)
    @ResponseBody
    public Result buildingExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_400, e.getMessage());
    }

    /**
     * 频繁操作异常处理
     */
    @ExceptionHandler(value = ThrottleException.class)
    @ResponseBody
    public Result throttleExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_403, e.getMessage());
    }


    /**
     * pg数据库异常处理
     */
    @ExceptionHandler(value = PSQLException.class)
    @ResponseBody
    public Result pSqlExceptionHandler(HttpServletRequest req, Exception e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_500, "参数异常，请检查参数！");
    }
      /**
     * pg数据库异常处理
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Result methodArgumentNotValidExceptionHandler(HttpServletRequest req, MethodArgumentNotValidException e) {
        logger.debug(e.getMessage(), e);
        return Result.response(Result.CODE_400, e.getBindingResult().getFieldError().getDefaultMessage());
    }




}

